<?php


namespace app\adminapi\lists\room\department;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\enum\DeviceEnum;
use app\common\lists\ListsExcelInterface;
use app\common\lists\ListsSearchInterface;
use app\common\model\room\department\RoomDepartment;
use app\common\model\room\department\RoomDepartmentDevice;
use app\common\model\room\department\RoomDepartmentDeviceLog;
use think\facade\Config;

/**
 * RoomDepartment列表
 * Class RoomDepartmentLists
 * @package app\adminapi\lists
 */
class RoomDepartmentLogLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExcelInterface {

    protected $export_name = '科室-导出';
    protected $export_lists = [];
    protected $export_fields = [];

    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function setSearch(): array {
        return [
            '=' => ['status'],
            '%like%' => ['name', 'department_sn'],
        ];
    }

    /**
     * @notes 搜索条件
     * @
     * @date 2023/2/24 16:08
     */
    public function queryWhere() {
        $where = [];

        if (!empty($this->adminCid) && $this->adminCid !== 0) {
            $where[] = ['cid', '=', $this->adminCid];
        }

        if (!empty($this->params['building'])) {
            if (isset($this->params['building'][0])) {
                $where[] = ['cid', '=', $this->params['building'][0]];
            }
            if (isset($this->params['building'][1])) {
                $where[] = ['bid', '=', $this->params['building'][1]];
            }
        }
        if (!empty($this->params['department'])) {
            if (isset($this->params['department'][0])) {
                $where[] = ['cid', '=', $this->params['department'][0]];
            }
            if (isset($this->params['department'][1])) {
                $where[] = ['bid', '=', $this->params['department'][1]];
            }
        }
        return $where;
    }


    public function get_query() {
        $this_month = date("Ym");
        $search_month = $this->request->param("search_month") > 0 ? $this->request->param("search_month") : date("Ym");

        return RoomDepartment::field([
            "*", "'" . $search_month . "' as log_date",
        ])->where($this->searchWhere)->where($this->queryWhere());
    }


    public function query_append($query, $is_page = 1) {
        $query = $query->with([
            "campus", "building", // "allDevice",
        ])->withSum(["allDevice" => function ($query, &$alias) {
            $alias = 'room_s_use_total';
            $query->where('device_type', 1);
        }], "use_total")->withSum(["allDevice" => function ($query, &$alias) {
            $alias = 'room_s_price';
            $query->where('device_type', 1);
        }], "price")->withSum(["allDevice" => function ($query, &$alias) {
            $alias = 'room_s_total_fee';
            $query->where('device_type', 1);
        }], "total_fee")->withSum(["allDevice" => function ($query, &$alias) {
            $alias = 'room_d_use_total';
            $query->where('device_type', 2);
        }], "use_total")->withSum(["allDevice" => function ($query, &$alias) {
            $alias = 'room_d_price';
            $query->where('device_type', 2);
        }], "price")->withSum(["allDevice" => function ($query, &$alias) {
            $alias = 'room_d_total_fee';
            $query->where('device_type', 2);
        }], "total_fee")->withSum(["allDevice" => function ($query, &$alias) {
            $alias = 'room_total_fee';
        }], "total_fee");

        if ($is_page == 1) {
            $query->limit($this->limitOffset, $this->limitLength);
        }

        return $query->select()->each(function (&$item) {
            $item->room_s_use_total = $item->room_s_use_total * 1;
            $item->room_s_price = $item->room_s_price * 1;
            $item->room_s_total_fee = $item->room_s_total_fee * 1;

            $item->room_d_use_total = $item->room_d_use_total * 1;
            $item->room_d_price = $item->room_d_price * 1;
            $item->room_d_total_fee = $item->room_d_total_fee * 1;

            $item->room_total_fee = 1 * sprintf("%.2f", $item->room_total_fee);
            return $item;
        })->toArray();
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function lists(): array {
        return $this->query_append($this->get_query());
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function count(): int {
        return ($this->get_query())->count();
    }


    public function print_lists() {
        return $this->lists();
    }

    public function setExport($name, $lists, $fields) {
        $this->export_name = $name;
        $this->export_lists = $lists;
        $this->export_fields = $fields;
    }

    /**
     * @notes 导出文件名
     * @return string
     * @author 段誉
     * @date 2022/11/24 16:17
     */
    public function setFileName(): string {
        return $this->export_name;
    }

    public function setExcelLists() {
        return $this->export_lists;
    }

    /**
     * @notes 导出字段
     * @return string[]
     * @author 段誉
     * @date 2022/11/24 16:17
     */
    public function setExcelFields(): array {
        return $this->export_fields;
    }

}
